import { Inject } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { CommunityCommentEntity } from '../../entity/comment';
import { CommunityCommentService } from '../../service/comment';
import { UserInfoEntity } from '../../../user/entity/info';
import { CommunityPostEntity } from '../../entity/post';

/**
 * 评论管理
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: CommunityCommentEntity,
  service: CommunityCommentService,
  pageQueryOp: {
    keyWordLikeFields: ['a.content'],
    fieldEq: ['a.postId'],
    fieldLike: [],
    where: async ctx => {
      const { nickName } = ctx.request.body;
      const where = [];
      if (nickName) {
        where.push([
          'b.nickName LIKE :nickName',
          { nickName: `%${nickName}%` },
        ]);
      }
      return where;
    },
    join: [
      {
        entity: UserInfoEntity,
        alias: 'b',
        condition: 'a.userId = b.id',
        type: 'leftJoin',
      },
      {
        entity: CommunityPostEntity,
        alias: 'c',
        condition: 'a.postId = c.id',
        type: 'leftJoin',
      },
    ],
    select: [
      'a.*',
      'b.nickName as nickName',
      'b.avatarUrl as avatarUrl',
      'c.content as postContent',
    ],
  },
})
export class AdminCommunityCommentController extends BaseController {
  @Inject()
  communityCommentService: CommunityCommentService;
}
